#-*- codig:UTF-8 -*-#
#-*- codig:UTF-8 -*-#
#!/usr/bin/python3

import _thread
from time import ctime
from time import sleep
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf8') #改变标准输出

def loop(i, sleep_time, lock):    # 参数依次为：标识，睡眠时间，锁对象
    sleep(sleep_time)
    print("输出", i, ": ", ctime())
    lock.release()  # 释放锁


if __name__ == '__main__':
    datas = [2,4,9]
    locks = []
    print("开始", ": ", ctime())

    for i in range(len(datas)):
        lock = _thread.allocate_lock()  # 分配锁对象
        lock.acquire()  # 获取锁对象
        locks.append(lock)

    for i in range(len(datas)):
        _thread.start_new(loop, (i, datas[i], locks[i]))

    # 等待所有锁被释放
    for i in range(len(datas)):
        while(locks[i].locked()):
            pass
    print('结束', ctime())
